package lyl.queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 元素加到queue2
 *
 * 遍历queue1把数据放到queue2的尾部
 * 交换指针
 * 读queue1就是栈
 */
public class ImplementStackByQueue {

    private Queue<Integer> queue1 = new LinkedList<>();
    private Queue<Integer> queue2 = new LinkedList<>();

    public void push(int x){
        queue2.add(x);
        while( !queue1.isEmpty()){
            queue2.add(queue1.poll());
        }
        Queue<Integer> q = queue1;
        queue1 = queue2;
        queue2 = q;
    }

    public int pop(){
        if(queue1.isEmpty()){
            throw new RuntimeException("null");
        }
        return queue1.poll();
    }

    public int peek(){
        if(queue1.isEmpty()){
            throw new RuntimeException("null");
        }
        return queue1.peek();
    }
}
